package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.*;
import cn.com.api.service.ApiU_againstService;
import cn.com.api.service.ApiU_circleService;
import cn.com.api.service.ApiU_circle_cateService;
import cn.com.api.service.ApiU_circle_memberService;
import cn.com.api.service.CheckService;
import cn.com.api.service.U_labelService;
import cn.com.util.*;

/**
 * 圈子控制器
 * 
 */
@Controller
@RequestMapping(Utils.SYS_API + "circle/")
public class ApiU_circleController {
	@Autowired
	private ApiU_circleService apiU_circleService;
	@Autowired
	private U_labelService u_labelService;
	@Autowired
	private ApiU_circle_memberService apiU_circle_memberService;
	@Autowired
	private ApiU_circle_cateService apiU_circle_cateService;
	@Autowired
	private ApiU_againstService againstService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiU_circleController.class);

	/**
	 * 创建小圈
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertU_circle")
	@Transactional
	public void insertU_circle(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String admin_nick, String circle_memberids, ApiU_circle u_circle) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiU_circleService.createCircle(user_id, admin_nick, circle_memberids, u_circle);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 圈主修改圈子信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editcircleinfo")
	public void editcircleinfo(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			ApiU_circle u_circle) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				ApiU_circle apiU_circle = apiU_circleService.findU_circleById(u_circle.getId());
				if (u_circle.getCircle_name() != null && !"".equals(u_circle.getCircle_name())) {
					apiU_circle.setCircle_name(u_circle.getCircle_name());
				}
				if (u_circle.getCircle_img() != null && !"".equals(u_circle.getCircle_img())) {
					apiU_circle.setCircle_img(u_circle.getCircle_img());
				}
				if (u_circle.getCircle_desc() != null && !"".equals(u_circle.getCircle_desc())) {
					apiU_circle.setCircle_desc(u_circle.getCircle_desc());
				}
				if (u_circle.getCircle_notice() != null && !"".equals(u_circle.getCircle_notice())) {
					apiU_circle.setCircle_notice(u_circle.getCircle_notice());
				}
				if (u_circle.getIs_need_confirm() != null && !"".equals(u_circle.getIs_need_confirm())) {
					apiU_circle.setIs_need_confirm(u_circle.getIs_need_confirm());
				}
				if (u_circle.getIs_show_name() != null && !"".equals(u_circle.getIs_show_name())) {
					apiU_circle.setIs_show_name(u_circle.getIs_show_name());
				}
				if (u_circle.getIs_show_address() != null && !"".equals(u_circle.getIs_show_address())) {
					apiU_circle.setIs_show_address(u_circle.getIs_show_address());
				}
				if (u_circle.getCircle_label() != null && !"".equals(u_circle.getCircle_label())) {
					apiU_circle.setCircle_label(u_circle.getCircle_label());
				}
				if (u_circle.getCate_id() != null && !"".equals(u_circle.getCate_id())) {
					apiU_circle.setCate_id(u_circle.getCate_id());
				}
				if (u_circle.getCate_name() != null && !"".equals(u_circle.getCate_name())) {
					apiU_circle.setCate_name(u_circle.getCate_name());
				}
				if (u_circle.getGroupid() != null && !"".equals(u_circle.getGroupid())) {
					apiU_circle.setGroupid(u_circle.getGroupid());
				}
				apiU_circleService.updateU_circle(apiU_circle);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取圈子档案
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getcirclearchives")
	public void getcirclearchives(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String circle_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				paramMap.clear();
				PageInfo pageInfo = new PageInfo();
				// paramMap.put(key, value)
				ApiU_circle apiU_circle = apiU_circleService.findU_circleById(Integer.valueOf(circle_id));
				paramMap.put("circle_id", circle_id);
				paramMap.put("user_id", user_id);
				Integer status = apiU_circle_memberService.isjoincircle(paramMap);
				apiU_circle.setIs_join(status);
				paramMap.clear();
				paramMap.put("circle_id", circle_id);
				paramMap.put("status", "1");
				pageInfo.setShowCount(6);
				pageInfo.setCurrentPage(1);
				List<ApiU_circle_member> memberlist = apiU_circle_memberService.getcirclemembersListPage(paramMap,
						pageInfo);
				Map<String, Object> result = new HashMap<String, Object>();
				result.put("circleinfo", apiU_circle);
				result.put("memberlist", memberlist);
				result.put("membersize", pageInfo.getTotalResult());
				// PageInfo pageInfo2 = new PageInfo();
				// pageInfo2.setShowCount(3);
				// pageInfo.setCurrentPage(1);
				// paramMap.clear();
				// paramMap.put("circle_id", circle_id);
				// List<ApiU_circle_post> posts =
				// apiU_circle_postService.findU_circle_postList(paramMap,
				// pageInfo2);
				// result.put("circlepostlist", posts);
				map.clear();
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, result);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取已加入圈子的数量
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "circle_addedcount")
	public void circle_addedcount(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				paramMap.clear();
				Integer count = apiU_circle_memberService.getcircle_addedcount(user_id);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, count);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * @Title: accusal
	 * @Description: 举报圈子/用户
	 * @param request
	 * @param response
	 * @param user_id
	 *            举报人id
	 * @param token
	 * @param user_name
	 *            举报人姓名
	 * @param accusal_id
	 *            举报目标的id
	 * @param accusalmsg
	 *            举报发送的内容
	 * @param type
	 *            1-用户；2-圈子
	 * @param against_id
	 *            举报信息的id，为空时添加；不为空时修改
	 * @throws Exception
	 * @return void
	 */
	@RequestMapping(value = "accusal")
	public void accusal(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String user_name, String accusal_id, String type, String accusalmsg, String against_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				if (against_id == null || "".equals(against_id)) {
					ApiU_against against = new ApiU_against();
					against.setAdd_time(DateUtil.nowTimeToString());
					against.setAgainst_uid(user_id);
					against.setConn_id(accusal_id);
					against.setType(Integer.valueOf(type));
					against.setReason(accusalmsg);
					against.setAgainst_uname(user_name);
					againstService.insertU_against(against);
				} else {
					ApiU_against against = againstService.findU_againstById(Integer.valueOf(against_id));
					against.setAdd_time(DateUtil.nowTimeToString());
					against.setReason(accusalmsg);
					againstService.updateU_against(against);
				}
				map.clear();
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	@RequestMapping(value = "getAccusalInfo")
	public void getAccusalInfo(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String accusal_id, String type) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				paramMap.clear();
				paramMap.put("accusal_id", accusal_id);
				paramMap.put("type", type);
				paramMap.put("user_id", user_id);
				ApiU_against against = againstService.findAgainst(user_id, accusal_id, type);
				map.clear();
				if (against != null) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作成功");
					map.put(Utils.DATAS, against);
				} else {
					map.put(Utils.MESSAGE, "没有数据");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除(解散)圈子
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "delcircle")
	public void delcircle(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String circle_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				map = apiU_circleService.delCircle(circle_id);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 查询圈子
	 * 
	 * @param str
	 *            keyword，关键字搜索（圈子名称或标签）
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchcircle")
	public void searchcircle(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String keyword, String cate_id, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				paramMap.clear();
				if (keyword != null && !"".equals(keyword)) {
					paramMap.put("keyword", keyword);
				}
				if (cate_id != null && !"".equals(cate_id)) {
					paramMap.put("cate_id", cate_id);
				}
				paramMap.put("followuser_id", user_id);
				List<ApiU_circle> list = apiU_circleService.findCirclesListPage(paramMap, pageInfo);
				map.clear();
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, list);
				map.put(Utils.Page, pageInfo);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取推荐的圈子
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "recommendcircle")
	public void recommendcircle(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				paramMap.clear();

				List<ApiU_circle> list = apiU_circleService.getRecommendcircleListPage(user_id, pageInfo);
				map.clear();
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, list);
				map.put(Utils.Page, pageInfo);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取附近的圈子
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "nearbycircle")
	public void nearbycircle(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String longitude, String latitude, String range, PageInfo pageInfo) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				paramMap.clear();
				paramMap.put("longitude", longitude);
				paramMap.put("latitude", latitude);
				if (range != null && !"".equals(range)) {
					paramMap.put("range", range);
				}
				paramMap.put("followuser_id", user_id);
				List<ApiU_circle> list = apiU_circleService.getNearbycircleListPage(paramMap, pageInfo);
				map.clear();
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, list);
				map.put(Utils.Page, pageInfo);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 查询圈子分类
	 * 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getcircirclecate")
	public void getcircirclecate(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String cate_id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);

			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				paramMap.clear();
				paramMap.put("parent_id", "-1");
				if (cate_id != null && !"".equals(cate_id)) {
					paramMap.put("cate_id", cate_id);
				}

				// String keyword = request.getParameter("keyword");
				// if (keyword != null) {
				// paramMap.put("cate_name", keyword);
				// }

				List<ApiU_circle_cate> list = apiU_circle_cateService.findCircleCatesByMap(paramMap);
				map.clear();
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, list);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取圈子标签
	 * 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getcircirclelabel")
	public void getcircirclelabel(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String labelnames) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				if (labelnames != null && !"".equals(labelnames)) {
					map.put("labels", labelnames.split(":"));
				}
				List<U_label> list = u_labelService.findAllLabels(map);
				map.clear();
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, list);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取我的圈子
	 * 
	 * @param type
	 *            0-加入的圈子；1-创建的圈子，2-关注
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getmycircles")
	public void getmycircles(HttpServletRequest request, HttpServletResponse response, String user_id, String token,
			String type, PageInfo pageInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				paramMap.clear();
				List<ApiU_circle> list = apiU_circleService.getmycircles(user_id, type, pageInfo);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作完成");
				map.put(Utils.DATAS, list);
				map.put(Utils.Page, pageInfo);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
